package com.h819.core.web.struts2;

/**
 * 自定义验证码验证器
 * 
 * <!--  add by apollo, 2008.09.11 -->
 */

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

import java.util.Map;

public final class ValidateCodeValidator extends FieldValidatorSupport {

	private boolean trim = true;

	public void validate(Object arg0) throws ValidationException {
		// 获得字段的名字
		String fieldName = getFieldName();
		// 获得输入界面输入的值
		String value = (String) this.getFieldValue(fieldName, arg0);

		Map session = (Map) ActionContext.getContext().get(
				ActionContext.SESSION);
		String rand = (String) session.get("validateCode");

		//需要增加入waterq
		if(rand==null || rand.equals("")){
			addFieldError(fieldName, arg0);
		}
		
		if (trim) {
			value = value.trim();
		}

		if (!rand.equals(value)) {
			addFieldError(fieldName, arg0);
		}
	}

	public boolean isTrim() {
		return trim;
	}

	public void setTrim(boolean trim) {
		this.trim = trim;
	}

}
